package com.rzj.sys_lab_assistant.controller;

import com.rzj.sys_lab_assistant.entity.Technician;
import com.rzj.sys_lab_assistant.service.ITechnicianService;
import com.rzj.sys_lab_assistant.utils.ResultsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @program: sys_lab_assistant
 * @description:
 * @author: 作者
 * @create: 2021-06-29 16:32
 */
@Controller
@RequestMapping("technician")
public class TechnicianController {

    @Autowired
    ITechnicianService technicianService;

    ResultsUtils resultsUtils;

    @RequestMapping("toAdd")
    public String toAdd() {
        return "technician/technicianAdd";
    }

    @RequestMapping("toEdit/{account}")
    public String toEdit(Model model, @PathVariable("account") String account) {
        model.addAttribute("technician", technicianService.getTechnicianById(account));
        return "technician/technicianUpdate";
    }

    @RequestMapping("technicianList")
    public String technicianList(Model model) {
        model.addAttribute("technicianList", technicianService.listTechnicians());
        return "technician/technicianList";
    }

    @RequestMapping("technicianAdd")
    @ResponseBody
    public Map<String, String> technicianAdd(@RequestParam("account") String account,
                                             @RequestParam("name") String name,
                                             @RequestParam("password") String password) {
        Technician technician = new Technician(account, name, password);
        int flag = technicianService.saveTechnician(technician);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "添加成功！");
        } else {
            return resultsUtils.resultsMap("error", "添加失败，请联系管理员！");
        }
    }

    @RequestMapping("technicianEdit")
    @ResponseBody
    public Map<String, String> technicianEdit(@RequestParam("account") String account,
                                             @RequestParam("name") String name,
                                             @RequestParam("password") String password) {
        Technician technician = new Technician(account, name, password);
        int flag = technicianService.updateTechnician(technician);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "添加成功！");
        } else {
            return resultsUtils.resultsMap("error", "添加失败，请联系管理员！");
        }
    }

    @RequestMapping("technicianDel")
    @ResponseBody
    public Map<String, String> technicianDel(@RequestParam("account") String account) {

        int flag = technicianService.deleteTechnician(account);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "删除成功！");
        } else {
            return resultsUtils.resultsMap("error", "删除失败，请联系管理员！");
        }

    }


}
